package com;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

//public class JUtil {
//    static String url;
//    static String user;
//    static String psd;
//    static {
//        Properties p=new Properties();
//        try {
//            p.load(new FileInputStream("D:\\mybits3\\src\\main\\resources\\jdbc.properties"));
//            String drivename=p.getProperty("drivename");
//            Class.forName(drivename);
//            url=p.getProperty("url");
//            user=p.getProperty("user");
//            psd=p.getProperty("psd");
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//
//
//    }
//
//    //建立连接
//    public static Connection getConn() throws SQLException {
//
//        return DriverManager.getConnection(url,user,psd);
//    }
//    public static void free(Statement st, Connection conn) throws SQLException {
//        st.close();
//        conn.close();
//    }
//    public static void free(ResultSet re, Statement st, Connection conn) throws SQLException {
//        re.close();
//        free(st,conn);
//    }
//}

public class JUtil {
    //1,注册驱动-Driver类，只注册一次
    static String url;
    static String user;
    static String psd;
    static{
        Properties p=new Properties();
        try {
            p.load(new FileInputStream("D:\\mybits3\\src\\main\\resources\\jdbc.properties"));

            String driverName=  p.getProperty("drivername");
            try {
                Class.forName(driverName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            url=p.getProperty("url");
            user=p.getProperty("user");
            psd=p.getProperty("psd");
        } catch (IOException e) {
            e.printStackTrace();
        }}

    //建立连接
    public static Connection getConn() throws SQLException {

        return DriverManager.getConnection(url,user,psd);
    }
    public static void free(Statement st,Connection conn) throws SQLException {
        st.close();
        conn.close();
    }
    public static void free(ResultSet re,Statement st, Connection conn) throws SQLException {
        re.close();
        free(st,conn);
    }
}